import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:redux/redux.dart';
import 'package:lai_zhan/common/redux/AppState.dart';
import 'package:lai_zhan/common/style/AppStyle.dart';
import 'package:lai_zhan/common/util/NavigatorUtil.dart';
import 'package:lai_zhan/widget/LzTextFormField.dart';
import 'package:lai_zhan/widget/EnsureVisibleWhenFocused.dart';
import 'package:lai_zhan/common/util/RegularUtil.dart';
import 'package:lai_zhan/widget/FlexButton.dart';
import 'package:lai_zhan/widget/CompactButton.dart';
import 'package:lai_zhan/common/dao/UserDao.dart';
import 'package:lai_zhan/common/dao/DataResult.dart';
import 'package:lai_zhan/widget/FlexOutLineButton.dart';
import 'package:lai_zhan/widget/BaseScaffold.dart';

class RegisterPage extends StatefulWidget {
  static String rName = 'register';

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new _RegisterPageState();
  }
}

class _RegisterPageState extends State<RegisterPage> {
  GlobalKey<FormState> _formKey = new GlobalKey<FormState>();
  TextEditingController phoneController = new TextEditingController();
  TextEditingController passwordController = new TextEditingController();
  TextEditingController rPasswordController = new TextEditingController();
  FocusNode _phoneFocusNode = new FocusNode();
  FocusNode _passwordFocusNode = new FocusNode();
  FocusNode _rPasswordFocusNode = new FocusNode();
  String _phone;
  String _password;
  String _rPassword;
  String _errorPhone;
  String _errorPwd;
  String _errorRPwd;

  @override
  Widget build(BuildContext context) {
    Store<AppState> store = StoreProvider.of(context);
    // TODO: implement build
    return new StoreBuilder(
        builder: (BuildContext context, Store<AppState> store) {
      return new BaseScaffold(
        title: '注册',
        iconColor: Colors.black,
        back: (){
          NavigatorUtil.goBack(context);
        },
        body: new GestureDetector(
          behavior: HitTestBehavior.translucent,
          onTap: () {
            FocusScope.of(context).requestFocus(new FocusNode());
          },
          child: Container(
              margin: EdgeInsets.all(20),
              child: new Theme(
                data: ThemeData(primaryColor: Color(AppColors.primaryColor)),
                child: new Form(
                    key: _formKey,
                    child: new ListView(
                      primary: false,
                      children: <Widget>[
                        new EnsureVisibleWhenFocused(
                          focusNode: _phoneFocusNode,
                          child: new LzTextFormField(
                            controller: phoneController,
                            labelText: "输入手机号",
                            icon: Icon(Icons.phone_android),
                            keyboardType: TextInputType.numberWithOptions(),
                            onSave: (val) => _phone = val,
                            validator: (val) {
                              if (val == null || val.trim().isEmpty) {
                                return "手机号不能为空";
                              } else if (!RegularUtil.isChinaPhoneLegal(
                                  val.trim())) {
                                return "请输入正确的手机号";
                              } else {
                                return null;
                              }
                            },
                          ),
                        ),
                        new EnsureVisibleWhenFocused(
                          focusNode: _passwordFocusNode,
                          child: new LzTextFormField(
                            controller: passwordController,
                            labelText: "密码(6-16字符 包括数字,字母)",
                            icon: Icon(Icons.vpn_key),
                            obscureText: true,
                            onSave: (val) => _password = val,
                            validator: (val) {
                              _password = val;
                              if (val == null || val.trim().isEmpty) {
                                return '密码为空';
                              } else if (val.trim().length < 6) {
                                return '密码长度小于6';
                              } else if (!RegularUtil.hasLetter(val)) {
                                print(val);
                                return '不存在字母';
                              } else if (!RegularUtil.hasNum(val)) {
                                return '不存在数字';
                              }
                              return null;
                            },
                          ),
                        ),
                        new EnsureVisibleWhenFocused(
                          focusNode: _rPasswordFocusNode,
                          child: new LzTextFormField(
                            controller: rPasswordController,
                            labelText: "确认密码",
                            icon: Icon(Icons.vpn_key),
                            obscureText: true,
                            onSave: (val) => _rPassword = val,
                            validator: (val) =>
                            val == _password ? null : '密码不一致',
                          ),
                        ),
                        new Padding(padding: EdgeInsets.only(top: 30)),
                        new FlexButton(
                          text: '注册',
                          textColor: Color(AppColors.white),
                          color: Color(AppColors.primaryColor),
                          onPress: () {
//                              var _form = _formKey.currentState;
//                              if (_form.validate()) {
//                                _form.save();
//                                //
//                              }
                            UserDao.register(_phone, _password, store)
                                .then((res) {
                              //还需要进行判断
                              NavigatorUtil.goComplete(context);
                            });
                          },
                        )
                      ],
                    )),
              )),
        ),
      );
    });
  }
}
